//
// Created by cxh on 2021/9/29.
//

#include "String.h"
String::String() {
    this->self = "";
}

String::String(const std::string& s){
    this->self = s;
}

String::String(const char* s) {
    this->self = s;
}

String String::operator+(const char *s) const{
    std::string nst = this->self + s;
    return String(nst);
}

String String::operator+(const String s) const{
    std::string nst = this->self + s.self;
    return String(nst);
}

String String::operator+(const long d) const{
    std::string nst = this->self + std::to_string(d);
    return String(nst);
}

void String::operator+=(const char *s) {
    this->self +=  s;
}

void String::operator+=(const std::string &s) {
    this->self +=  s;
}

void String::operator+=(const String &s) {
    this->self +=  s.self;
}

bool String::operator==(const String &s) const {
    if(this->self.length() != s.self.length()){
        return false;
    }
    for (int i = 0; i < this->self.length(); ++i) {
        if(this->self[i] != s.self[i]){
            return false;
        }
    }
    return true;
}

/**
 * 字符串长度
 * @return
 */
int String::length() {
    return self.length();
}

/**
 * 判断为空
 * @return 结果
 */
bool String::empty() {
    return this->self == "" || length() == 0;
}


/**
* 转为 char*
* @return char*
*/
const char * String::c_str() const {
    return this->self.c_str();
}

/**
* 转为 char*
* @return char*
*/
char * String::toC_str() {
    return const_cast<char *> (this->self.c_str());
}


/**
 * 截取字符串，原字符串不变
 * @param start 起始索引
 * @param end 终止索引
 * @return 截取后字符串
 */
String String::subStr(unsigned int start,unsigned int end) {

    if(end > this->length()){
        throw "字符串索越界";
    }

    std::string cp;
    for(int i = start; i < end; i++){
        cp += this->self[i];
    }

    return cp;
}


/**
 * 字符串分割
 * @param splitStr 分隔符
 * @param outArray 分割后数组
 */
void String::split(String splitStr, std::vector<String> &outArray) {
    if(splitStr.empty()){
        outArray.push_back(*this);
        return;
    }

    int posStart, posEnd;
    //记录分隔符的长度
    int len = splitStr.length();
    posStart = 0;
    posEnd= this->self.find(splitStr.c_str());
    while(posEnd != std::string::npos )
    {
        outArray.push_back( this->self.substr(posStart, posEnd - posStart) );
        posStart = posEnd + len;
        // 从str的pos1位置开始搜寻spacer
        posEnd = this->self.find(splitStr.c_str(), posStart);
    }
    if(posStart != this->self.length()) //分割最后一个部分
        outArray.push_back(this->self.substr(posStart));
}


/**
* 字符串分割,支持正则表达式
* @param regex 分隔符,支持正则表达式
* @param outArray 分割后数组
*/

void String::splitWithRegex(String regex, std::vector<String> &outArray) {
    std::regex ex(regex.c_str());

    unsigned int startPos = 0;
    for (std::sregex_iterator it(this->self.begin(), this->self.end(), ex), end;  //end是尾后迭代器，regex_iterator是regex_iterator的string类型的版本
         it != end;
         ++it)
    {
        unsigned int pos = it->position();
        unsigned int len = it->length();
        if(startPos != pos){
            outArray.emplace_back(this->subStr(startPos, it->position()));
        }
        startPos = pos + len;
    }
    if(startPos != this->self.length()) //分割最后一个部分
        outArray.emplace_back(this->self.substr(startPos));
}

/**
 * 字符在字符串中最开始出现的索引值
 * @param c 字符
 * @return 索引，如果不存在返回 -1
 */
int String::indexOf(char c) {
    int len = self.length();
    for (int i = 0; i < len; i++) {
        if(c == self[i]){
            return i;
        }
    }

    return -1;
}

/**
 * 字符在字符串中最后出现的索引值
 * @param c 字符
 * @return 索引，如果不存在返回 -1
 */
int String::lastIndexOf(char c) {
    int index = -1;

    int len = self.length();
    for (int i = 0; i < len; i++) {
        if(c == self[i]){
            index = i;
        }
    }

    return index;
}

/**
* 获得类名
*/
String String::getClassName(String filePath) {
    int s = filePath.lastIndexOf('\\');
    int e = filePath.lastIndexOf('.');
    if(e > s && s + 1 <= e){
        return filePath.subStr(s + 1, e);
    }
    return "";
}

/**
 * 获取文件名+后缀
 * @param filePath 文件路径
 * @return 文件名+后缀
 */
String String::getFileName(String filePath) {
    int s = filePath.lastIndexOf('\\');
    int e = filePath.length();
    if(e > s && s + 1 <= e){
        return filePath.subStr(s + 1, e);
    }
    return "";
}


/**
 * int 转 字符串
 * @param i 数字
 * @return 字符串
 */
String String::toString(int i) {
    char c[64];
    sprintf(c, "%d", i);
    return String(c);
}

/**
 * float 转 字符串
 * @param f 数字
 * @return 字符串
 */
String String::toString(float f) {
    char c[64];
    if(f >= 0){
        sprintf(c, "%.4f", f);
    }else{
        sprintf(c, "%.3f", f);
    }
    return String(c);
}

/**
 * 格式化字符串
 * @param fmt 模板
 * @param ... 变量
 * @return 格式化字符串
 */
String String::toString(const char *fmt, ...) {
    char msg[STR_LENGTH];
    va_list arg;
    va_start(arg, fmt);
    _vsnprintf(msg, STR_LENGTH, fmt, arg);
    va_end(arg);

    return String(msg);
}

/**
 * 获得原始字符串
 * @return
 */
const std::string &String::getSelf() const {
    return self;
}












